//
//  TestModeViewController.m
//  Multiview
//
//  Created by Moo Chewachatrekasem on 10-03-09.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "TestModeViewController.h"
#import "MultiviewAppDelegate.h"

NSString *word;
NSArray *wordList;
int wrongAnswer;


@implementation TestModeViewController




- (IBAction) returnToMenu {
	[self displayResult];
	MultiviewAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	[appDelegate displayView:2];
}

- (IBAction) playSound {
	
	if (sound != nil)
		[sound release];
	
	NSLog(@"%@", word);
	NSString *soundFile = [NSString stringWithFormat:@"%@%@", word, @".wav"];	
	//[NSString stringWithFormat:@"%@.wav", word];
	NSLog(@"%@", soundFile);
	
	sound = [[itootSound alloc] initWithFileName:soundFile];
	[sound play];	
	
}

- (IBAction) submit {
	currentQuestion = [question.text intValue];
	int nextQuestion = currentQuestion + 1;
	
	NSLog(@"%@ %@", answer.text, word);
	
	if ([answer.text isEqualToString: word]){
		rightAnswer++;
	}else{
		wrongAnswer++;
	}
	
	
	
	[word release];
	
	if (nextQuestion > totalQuestion) {
		[self endTest];
	}else {
		answer.text = nil;
	}

	
	
	
	
	
	word = [[NSString alloc] initWithString:[wordList objectAtIndex: nextQuestion]];
	
	question.text = [NSString stringWithFormat:@"%d", nextQuestion]; 
}

- (void) endTest{
	//[wordList release];
	//[self displayResult];
	[self returnToMenu];
	
}

- (int) getTotalQuestion {
	return 10;
}

- (void) displayResult{
	NSString *rightMessage = [@"Right: " stringByAppendingString:[NSString stringWithFormat:@"%d", rightAnswer]];
	NSString *wrongMessage = [@"Wrong: " stringByAppendingString:[NSString stringWithFormat:@"%d", 10 - rightAnswer]];
	NSString *resultMessage = [NSString stringWithFormat:@"%@ %@", rightMessage, wrongMessage];
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test Ended" message:[NSString stringWithFormat:@"%@",resultMessage]
												   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
	[alert show];
	[alert release];

}

- (void) createWordList {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];

	
	//get path to your file
	NSString *myPathDocs =  [documentsDirectory stringByAppendingPathComponent:@"wordlist.txt"];
	
	if (![[NSFileManager defaultManager] fileExistsAtPath:myPathDocs])
	{
		NSString *myPathInfo = [[NSBundle mainBundle] pathForResource:@"wordlist" ofType:@"txt"];
		NSFileManager *fileManager = [NSFileManager defaultManager];
	}
	
	NSString *myString = [[NSString alloc] initWithContentsOfFile:myPathDocs encoding:NSUTF8StringEncoding error:NULL];
	
	NSLog(@"ASSAASAS %@", myString);
	
	
	wordList = [myString componentsSeparatedByString:@"\n"];
	NSLog(@"ASSAASAS %@", [wordList objectAtIndex: 0]);
	[myString release];
	
}
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	totalQuestion = [self getTotalQuestion];
	rightAnswer = 0;
	wrongAnswer = 0;
	
	
	
	[self createWordList];
	
	word = [[NSString alloc] initWithString:[wordList objectAtIndex: 0]];
	[wordList retain];
	NSLog(@"word: %@", word);
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
		// Releases the view if it doesn't have a superview.
	[super didReceiveMemoryWarning];
		
		// Release any cached data, images, etc that aren't in use.
}
	

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[sound release];
	[question release];
    [super dealloc];
}


@end
